//
//  SHTakePhotoCell.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2018/1/29.
//  Copyright © 2018年 SH_iOS. All rights reserved.
//

#import "SHTakePhotoCell.h"
@interface SHTakePhotoCell()
@property (nonatomic,strong)UIView *borderBgView;
@property (nonatomic,strong)UIButton *takePhotoBtn;
@property (nonatomic,strong)SHLabel *titleLabel;
@property (nonatomic,strong)UIImageView *photoImageView;


@end
@implementation SHTakePhotoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = KBASEBGCOLOR;
        [self.contentView addSubview:self.borderBgView];
        [self.contentView addSubview:self.photoImageView];
        [self.contentView addSubview:self.takePhotoBtn];
        [self.contentView addSubview:self.titleLabel];

        [self.takePhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).with.offset(__I6Height(20));
                make.centerX.mas_equalTo(self.contentView.mas_centerX);
                make.width.height.mas_equalTo(__I6Width(70));
            
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.takePhotoBtn.mas_bottom).with.offset(__I6Height(-5));
                make.centerX.mas_equalTo(self.contentView.mas_centerX);
                make.height.mas_equalTo(__I6Height(13));
        }];
        
        [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).with.offset(__I6Height(0));
                make.centerX.mas_equalTo(self.contentView.mas_centerX);
                make.width.mas_equalTo(__I6Width(335));
                make.height.mas_equalTo(__I6Height(121));

        }];
        
    }
    return self;
}
- (UIView *)borderBgView{
    
    if (!_borderBgView) {
        _borderBgView = [[UIView alloc]init];
        _borderBgView.backgroundColor = [UIColor whiteColor];
        _borderBgView.frame = CGRectMake(__I6Width(20), 0, __I6Width(335), __I6Height(121));
       
         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: _borderBgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(__I6Width(6),__I6Width(6))];
        
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = _borderBgView.bounds;
        borderLayer.lineWidth = 0.5;
        borderLayer.strokeColor = KLINECOLOR.CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        

        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = _borderBgView.bounds;
        maskLayer.path = maskPath.CGPath;
        borderLayer.path = maskPath.CGPath;
        
        [_borderBgView.layer insertSublayer:borderLayer atIndex:0];
        _borderBgView.layer.mask = maskLayer;



//        _borderBgView.layer.cornerRadius = __I6Width(6);
//        _borderBgView.layer.masksToBounds = YES;
//        [_borderBgView setCornerRadius:__I6Height(6) bounds:_borderBgView.bounds];
//        _borderBgView.layer.borderWidth = 0.5;
//        _borderBgView.layer.borderColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
      
    
       
    }
    
    return _borderBgView;
}
- (UIButton *)takePhotoBtn{
    if (!_takePhotoBtn) {
        _takePhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_takePhotoBtn setImage:[UIImage imageNamed:@"hanpay_take_photo@2x"] forState:UIControlStateNormal];
    
        [_takePhotoBtn addTarget:self action:@selector(takePhotoBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _takePhotoBtn;
}
- (SHLabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [SHLabel labelWithTextFont:13 textColor:[UIColor blackColor] textAlignment:NSTextAlignmentCenter];
//        _titleLabel.shadowOffset = CGSizeMake(1, 1);
//        _titleLabel.shadowColor = [UIColor whiteColor];

        _titleLabel.text = @"拍照";
    }
    
    return _titleLabel;
}

- (UIImageView *)photoImageView{
    
    if (!_photoImageView) {
        
        _photoImageView = [[UIImageView alloc]init];
        _photoImageView.userInteractionEnabled = NO;
        _photoImageView.contentMode = UIViewContentModeScaleAspectFill;
        _photoImageView.clipsToBounds = YES;
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: _borderBgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(__I6Width(6),__I6Width(6))];
        
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = _borderBgView.bounds;
        borderLayer.lineWidth = 0.5;
        
        borderLayer.strokeColor = KLINECOLOR.CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = _borderBgView.bounds;
        maskLayer.path = maskPath.CGPath;
        borderLayer.path = maskPath.CGPath;
        
        [_photoImageView.layer insertSublayer:borderLayer atIndex:0];
        _photoImageView.layer.mask = maskLayer;

    }
    
    return _photoImageView;
}
- (void)takePhotoBtnAction:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(takePhotoCell:takePhotoButton:)]) {
        
        [_delegate takePhotoCell:self takePhotoButton:sender];
    }
    
}

- (void)setButtonTag:(NSInteger)buttonTag{
    _buttonTag = buttonTag ;
    self.takePhotoBtn.tag = buttonTag ;
}
- (void)setImageUrl:(NSString *)imageUrl{
    
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil];
    if (imageUrl) {
        self.titleLabel.textColor = [UIColor whiteColor];
    }

}

- (void)setTitleString:(NSString *)titleString{
    
    self.titleLabel.text = titleString;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
